package cn.lixufeng.pet.service.impl;

import cn.lixufeng.basic.service.impl.BaseServiceImpl;
import cn.lixufeng.basic.utils.DistanceUtil;
import cn.lixufeng.basic.utils.PageList;
import cn.lixufeng.basic.utils.Point;
import cn.lixufeng.org.domain.Employee;
import cn.lixufeng.org.domain.Shop;
import cn.lixufeng.org.mapper.EmployeeMapper;
import cn.lixufeng.org.mapper.ShopMapper;
import cn.lixufeng.pet.domain.Pet;
import cn.lixufeng.pet.domain.SearchMasterMsg;
import cn.lixufeng.pet.mapper.SearchMasterMsgMapper;
import cn.lixufeng.pet.query.SearchMasterMsgQuery;
import cn.lixufeng.pet.service.IPetService;
import cn.lixufeng.pet.service.ISearchMasterMsgService;
import cn.lixufeng.user.domain.User;
import cn.lixufeng.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

     @Autowired
     private UserMapper userMapper;

     @Autowired
     private ShopMapper shopMapper;

     @Autowired
     private SearchMasterMsgMapper searchMasterMsgMapper;

     @Autowired
     private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;

    /**
     * 发布寻主消息
     * @param searchMasterMsg
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long loginInfoId) {
        // 获取当前登录者
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        searchMasterMsg.setUser_id(user.getId());
        searchMasterMsg.setUser(user);
        // 获取最近的店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.findAll());
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 保存寻主消息
        searchMasterMsgMapper.add(searchMasterMsg);

    }

    /**
     * 待处理消息
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        // 获取登录者
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        // 如果登录者有有商家id
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        // 查询待处理消息，state为0
        query.setState(0);
        return super.findPage(query);
    }

    /**
     * 已处理消息
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        // 获取登录者
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        // 如果登录者有有商家id
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        // 查询待处理消息，state为1
        query.setState(1);
        return super.findPage(query);
    }

    /**
     * 查询个人中心
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> findUserMsg(SearchMasterMsgQuery query, Long id) {
        // 获取当前登录者
        User user = userMapper.loadByLoginInfoId(id);
        query.setUserId(user.getId());
        return super.findPage(query);
    }

    /**
     * 处理消息
     * @param pet
     * @param id
     */
    @Override
    @Transactional
    public void handle(Pet pet, Long id) {
        // 修改消息状态
        searchMasterMsgMapper.updateStateHandlerById(pet.getSearch_master_msg_id());
        // 新增宠物和详情
        petService.add(pet);
        // @Todo 生成订单并支付

    }

    //    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}